package com.hdjk.view.callback;

import com.hdjk.view.entity.HoldingFundEntity;
import com.lst.projectlib.entity.Msg;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by sxy on 2017/11/2.
 */

public abstract class GetHoldingFundCallBack extends MsgCallBack {
    @Override
    public Msg parseNetworkResponse(Response response, int i) throws Exception {
        Msg msg = super.parseNetworkResponse(response, i);
        JSONObject object = (JSONObject) msg.getData();
        if (object != null) {
            List<HoldingFundEntity> list = new ArrayList<>();
            if (!object.isNull("data")) {
                JSONArray data = object.getJSONArray("data");
                if (data != null) {
                    for (int j = 0; j < data.length(); j++) {
                        JSONObject jsonObject = data.getJSONObject(j);

                        if (!jsonObject.isNull("modelPortfolioCode")) {
                            if (!jsonObject.isNull("fundHoldings")) {
                                JSONObject fundHoldings = jsonObject.getJSONObject("fundHoldings");
                                if (!fundHoldings.isNull("data")) {
                                    JSONArray funds = fundHoldings.getJSONArray("data");
                                    if (funds != null) {
                                        HoldingFundEntity entity = new HoldingFundEntity();
                                        entity.setGroup(true);
                                        if (!jsonObject.isNull("portfolioId"))
                                            entity.setPortfolioId(jsonObject.getString("portfolioId"));

                                        if (!jsonObject.isNull("portfolioName")) {
                                            entity.setFundName(jsonObject.getString("portfolioName"));
                                        }
                                        BigDecimal bdcurrentValue = BigDecimal.ZERO;
                                        BigDecimal bdpreviousProfitNLoss = BigDecimal.ZERO;
                                        BigDecimal bdnav = BigDecimal.ZERO;
                                        for (int k = 0; k < funds.length(); k++) {
                                            JSONObject fund = funds.getJSONObject(k);
                                            if (!fund.isNull("currentValue")) {
                                                double l = fund.getDouble("currentValue");
                                                BigDecimal b = new BigDecimal(l);
                                                bdcurrentValue = bdcurrentValue.add(b);
                                            }

                                            if (!fund.isNull("previousProfitNLoss")) {
                                                double l = fund.getDouble("previousProfitNLoss");
                                                BigDecimal b = new BigDecimal(l);
                                                bdpreviousProfitNLoss = bdpreviousProfitNLoss.add(b);
                                            }
                                            if (!fund.isNull("nav")) {
                                                double l = fund.getDouble("nav");
                                                BigDecimal b = new BigDecimal(l);
                                                bdnav = bdnav.add(b);
                                            }
                                        }
                                        entity.setSize(funds.length());
                                        entity.setCurrentValue(bdcurrentValue.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                                        entity.setNav(bdnav.setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                                        entity.setPreviousProfitNLoss(bdpreviousProfitNLoss.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                                        list.add(entity);
                                    }
                                }
                            }
                        } else {
                            if (!jsonObject.isNull("fundHoldings")) {
                                JSONObject fundHoldings = jsonObject.getJSONObject("fundHoldings");
                                if (!fundHoldings.isNull("data")) {
                                    JSONArray funds = fundHoldings.getJSONArray("data");
                                    if (funds != null) {
                                        String portfolioId = "";
                                        if (!jsonObject.isNull("portfolioId"))
                                            portfolioId = jsonObject.getString("portfolioId");

                                        for (int k = 0; k < funds.length(); k++) {
                                            JSONObject fund = funds.getJSONObject(k);
                                            HoldingFundEntity entity = new HoldingFundEntity();
                                            entity.setSize(1);
                                            entity.setPortfolioId(portfolioId);
                                            entity.setGroup(false);
                                            if (!fund.isNull("fundCode")) {
                                                entity.setFundCode(fund.getString("fundCode"));
                                            }
                                            if (!fund.isNull("fundName")) {
                                                entity.setFundName(fund.getString("fundName"));
                                            }
                                            if (!fund.isNull("fundType")) {
                                                entity.setFundType(fund.getString("fundType"));
                                            }
                                            if (!fund.isNull("mainCode")) {
                                                entity.setMainCode(fund.getString("mainCode"));
                                            }
                                            if (!fund.isNull("chargeMode")) {
                                                entity.setChargeMode(fund.getString("chargeMode"));
                                            }
                                            if (!fund.isNull("dividendInstruction")) {
                                                entity.setDividendInstruction(fund.getString("dividendInstruction"));
                                            }
                                            if (!fund.isNull("totalUnit")) {
                                                double l = fund.getDouble("totalUnit");
                                                BigDecimal b = new BigDecimal(l);
                                                entity.setTotalUnit(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                                            }
                                            if (!fund.isNull("availableUnit")) {
                                                double l = fund.getDouble("availableUnit");
                                                BigDecimal b = new BigDecimal(l);
                                                entity.setAvailableUnit(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                                            }
                                            if (!fund.isNull("currentValue")) {
                                                entity.setCurrentValue(fund.getString("currentValue"));
                                            }
                                            if (!fund.isNull("investmentAmount")) {
                                                entity.setInvestmentAmount(fund.getString("investmentAmount"));
                                            }
                                            if (!fund.isNull("profitNLoss")) {
                                                entity.setProfitNLoss(fund.getString("profitNLoss"));
                                            }
                                            if (!fund.isNull("previousProfitNLoss")) {
                                                double l = fund.getDouble("previousProfitNLoss");
                                                BigDecimal b = new BigDecimal(l);
                                                entity.setPreviousProfitNLoss(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                                            }
                                            if (!fund.isNull("undistributeMonetaryIncome")) {
                                                entity.setUndistributeMonetaryIncome(fund.getString("undistributeMonetaryIncome"));
                                            }
                                            if (!fund.isNull("dividendCash")) {
                                                double l = fund.getDouble("dividendCash");
                                                BigDecimal b = new BigDecimal(l);
                                                entity.setDividendCash(b.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                                            }
                                            if (!fund.isNull("nav")) {
                                                double l = fund.getDouble("nav");
                                                BigDecimal b = new BigDecimal(l);
                                                entity.setNav(b.setScale(4, BigDecimal.ROUND_HALF_UP).toString());
                                            }
                                            if (!fund.isNull("navDate")) {
                                                entity.setNavDate(fund.getString("navDate"));
                                            }
                                            if (!fund.isNull("intransitAssets")) {
                                                entity.setIntransitAssets(fund.getString("intransitAssets"));
                                            }
                                            if (!fund.isNull("buyEnable")) {
                                                entity.setBuyEnable(fund.getString("buyEnable"));
                                            }
                                            if (!fund.isNull("sellEnable")) {
                                                entity.setSellEnable(fund.getString("sellEnable"));
                                            }
                                            if (!fund.isNull("switchEnable")) {
                                                entity.setSwitchEnable(fund.getString("switchEnable"));
                                            }
                                            list.add(entity);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            msg.setData(list);

        }
        return msg;
    }
}
